<div class="layui-card">
	<div class="layui-card-header">
		<h2 class="header-title">机构管理</h2>
		<span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>机构管理</cite></a>
        </span>
	</div>
	<div class="layui-card-body">
		<div class="layui-form toolbar">
			<button id="user-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
		</div>

		<table class="layui-table" id="org-table" lay-filter="org-table"></table>
	</div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="org-table-bar">
	<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
	<a class="layui-btn layui-btn-xs" lay-event="reset">删除</a>
</script>
<script type="text/javascript" src="module/common.js"></script>
<!-- 表格状态列 -->
<script type="text/html" id="org-tpl-state">
	<input type="checkbox" lay-filter="org-tpl-state" value="{{d.orgId}}" lay-skin="switch" lay-text="正常|锁定" {{d.status==1? 'checked': ''}}/>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="org-model">
	<form id="org-form" lay-filter="org-form" class="layui-form model-form">
		<input name="orgId" hidden="hidden" />
		<div class="layui-form-item">
			<label class="layui-form-label">机构名称</label>
			<div class="layui-input-block">
				<input name="name" placeholder="请输入机构名称" type="text" class="layui-input" lay-verify="required" required/>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">机构编码</label>
			<div class="layui-input-block">
				<input name="code" placeholder="请输入机构编码" type="text" class="layui-input" lay-verify="required" required/>
			</div>
		</div>
		<div class="layui-form-item model-form-footer">
			<button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
			<button class="layui-btn" lay-filter="org-form-submit" lay-submit>保存</button>
		</div>
	</form>
</script>

<script>
	layui.use(['form', 'table', 'util', 'config', 'admin'], function() {
		var form = layui.form;
		var table = layui.table;
		var config = layui.config;
		var layer = layui.layer;
		var util = layui.util;
		var admin = layui.admin;
		showOrg()

		function showOrg() {
			$.ajax({
				type: "POST",
				url: path + '/sys/org/list1',
				dataType: "json",
				xhrFields: {
					withCredentials: true
				},
				beforeSend: function(request) {},
				success: function(data) {
					// 渲染表格
					table.render({
						elem: '#org-table',
						data: data,
						page: true,
						cols: [
							[{
									field: 'orgId',
									title: '机构ID'
								},
								{
									field: 'name',
									title: '机构名称'
								},
								{
									field: 'code',
									title: '机构编码'
								},
								{
									field: 'status',
									templet: '#org-tpl-state',
									title: '状态'
								},
								{
									align: 'center',
									toolbar: '#org-table-bar',
									title: '操作'
								}
							]
						]
					});

				},
				beforeSend: function() {},
				complete: function() {},

				error: function(result) {
					console.log(result);
				},
			})
		}

		//parentId  name code  orderNum status

		// 添加按钮点击事件
		$('#user-btn-add').click(function() {
			showEditModel();
		});

		// 工具条点击事件
		table.on('tool(org-table)', function(obj) {
			var data = obj.data;
			var layEvent = obj.event;

			if(layEvent === 'edit') { // 修改
				showEditModel(data);
			} else if(layEvent === 'reset') { // 重置密码
				layer.confirm('确定要删除吗？', function(i) {
					layer.close(i);
					var x = [obj.data.orgId]
					$.ajax({
						type: "POST",
						url: delOrg,
						traditional: true,
						data: {
							id: x
						},
						dataType: "json",
						xhrFields: {
							withCredentials: true
						},
						success: function(data) {
							console.log(data);
							if(data.code == 0) {
								layer.closeAll();
								layer.msg(data.msg, {
									icon: 1
								});
								showOrg()
							} else {
								layer.msg(data.msg, {
									icon: 2
								});
							}
						},
						error: function(result) {
							console.log(result);
						},
					});
				});
			}
		});

		// 表单提交事件
		form.on('submit(org-form-submit)', function(data) { //data.field
			var hhh = $(".zbyorg .layui-layer-title").text();
			console.log(data.field)

			if(hhh == "添加机构") {
				var a = {
					parentId: 0,
					name: data.field.name,
					code: data.field.code,
					orderNum: 1,
					status: 1
				}
				orgau(savaOrg, a)

			} else if(hhh == "修改机构") {
				var a = {
					orgId: data.field.orgId,
					name: data.field.name,
					code: data.field.code,
				}
				orgau(updateOrg, a)
			}
			return false;
		});

		//显示表单弹窗
		var showEditModel = function(datas) {
			layer.open({
				type: 1,
				title: datas ? '修改机构' : '添加机构',
				skin: 'zbyorg',
				area: '450px',
				offset: '120px',
				content: $('#org-model').html(),
				success: function() {
					$('#org-form')[0].reset();
					$('#org-form').attr('method', 'GET');
					if(datas) {
						form.val('org-form', datas);
						$('#org-form').attr('method', 'GET');
					}
				}
			});

		};

		function orgau(paths, datas) {
			$.ajax({
				type: "POST",
				url: paths,
				data: datas,
				dataType: "json",
				xhrFields: {
					withCredentials: true
				},
				success: function(data) {
					if(data.code == 0) {
						layer.closeAll();
						layer.msg(data.msg, {
							icon: 1
						});
						showOrg()
					} else {
						layer.msg(data.msg, {
							icon: 2
						});
					}
				},
				complete: function() {},
				error: function(result) {
					console.log(result);
				},
			});
		}

		// 搜索按钮点击事件
		$('#user-btn-search').click(function() {});

		// 修改user状态
		form.on('switch(org-tpl-state)', function(obj) {
			layer.load(2);
			//			orgId: obj.elem.value,
			//			state: obj.elem.checked ? 0 : 1
		});
	});
</script>